package ejercicios;

import interfaces.Node;
import interfaces.tests.TraverseTreeInDepth;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TraverseTreeInDepthImpl implements TraverseTreeInDepth {
	public List<Node> traverseTreeInDepth(Node root) {
    	if (root == null){
    		return new ArrayList<Node>();
    	}
    	
    	List<Node> result = new ArrayList<Node>();
    	List<Node> currentChildren = Arrays.asList(root);
    	List<Node> newChildren;
    	
    	while(!currentChildren.isEmpty()){
    		newChildren = new ArrayList<Node>();
    		for (Node node : currentChildren){
    			result.add(node);
    			newChildren.addAll(node.getChildren());
    		}
    		currentChildren = newChildren;
    	}
    	
    	return result;
    }
}